package com.premedsidekick;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.EditText;


public class DefaultEntryCursorAdapter extends CursorAdapter {
	Context context;
	View view;
	LayoutInflater inflater;
	DefaultEntryDatabaseAdapter dbHelper;
	
	public DefaultEntryCursorAdapter(Context context, Cursor cursor, DefaultEntryDatabaseAdapter dbHelper) {
		super(context, cursor);
		this.context = context;
		this.dbHelper = dbHelper;
		inflater = LayoutInflater.from(context);
	}
	public void initialize(DefaultEntryDatabaseAdapter dbHelper, Cursor cursor) {
		this.dbHelper = dbHelper;
		super.changeCursor(cursor);
		
	}
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		final Cursor c = cursor;
		view = inflater.inflate(R.layout.award_list, null);
		int id = cursor.getColumnIndex(dbHelper.KEY_ROWID);
		String key = cursor.getString(id);
		final int index = Integer.parseInt(key);
		
		int nameCol = cursor.getColumnIndex(dbHelper.KEY_NAME);
		String name = cursor.getString(nameCol);
		final EditText edittext = (EditText)view.findViewById(R.id.award_edittext);
		edittext.setText(name);
		edittext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				edittext.selectAll();
			}
			
		});
		/*
		edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
	        @Override
	        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
	            if (actionId == EditorInfo.IME_ACTION_DONE) {
	            	dbHelper.update(index, edittext.getText().toString());
		        	c.requery();
	                return true;
	            }
	            return false;
	        }
	    });
		*/
		edittext.setOnKeyListener(new OnKeyListener() {
		    public boolean onKey(View view, int keyCode, KeyEvent event) {
		        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
		            (keyCode == KeyEvent.KEYCODE_ENTER)) {
		        	dbHelper.update(index, edittext.getText().toString());
		        	c.requery();
		        	return true;
		        }
		        return false;
		    }
		});
		
		view.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View view) {
				AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
				builder.setMessage("Are you sure you want to delete this entry?")
				       .setCancelable(false)
				       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				                dbHelper.delete(index);
				                c.requery();
				           }
				       })
				       .setNegativeButton("No", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				                dialog.cancel();
				           }
				       });
				AlertDialog alert = builder.create();
				alert.show();
				return true;
			}
        });
		return view;
	}
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		final Cursor c = cursor;
		
		int id = cursor.getColumnIndex(dbHelper.KEY_ROWID);
		String key = cursor.getString(id);
		final int index = Integer.parseInt(key);
		
		int nameCol = cursor.getColumnIndex(dbHelper.KEY_NAME);
		String name = cursor.getString(nameCol);
		final EditText edittext = (EditText)view.findViewById(R.id.award_edittext);
		edittext.setText(name);
		edittext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				edittext.selectAll();
			}
			
		});
		/*
		edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
	        @Override
	        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
	            if (actionId == EditorInfo.IME_ACTION_DONE) {
	            	dbHelper.update(index, edittext.getText().toString());
		        	c.requery();
	                return true;
	            }
	            return false;
	        }
	    });
		*/
		edittext.setOnKeyListener(new OnKeyListener() {
		    public boolean onKey(View view, int keyCode, KeyEvent event) {
		        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
		            (keyCode == KeyEvent.KEYCODE_ENTER)) {
		        	dbHelper.update(index, edittext.getText().toString());
		        	c.requery();
		        	return true;
		        }
		        return false;
		    }
		});
		
		view.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View view) {
				AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
				builder.setMessage("Are you sure you want to delete this entry?")
				       .setCancelable(false)
				       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				                dbHelper.delete(index);
				                c.requery();
				           }
				       })
				       .setNegativeButton("No", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				                dialog.cancel();
				           }
				       });
				AlertDialog alert = builder.create();
				alert.show();
				return true;
			}
        });
	}
}
